package com.example.service.impl;


import com.example.constant.CrowdConstant;
import com.example.entity.po.MemberPO;
import com.example.entity.po.MemberPOExample;
import com.example.exception.LoginAcctAlreadyInUseException;
import com.example.mapper.MemberPOMapper;
import com.example.service.api.MemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//@Transactional(readOnly = true)
@Service

public class MemberServiceimpl implements MemberService {

    @Autowired
    private MemberPOMapper memberPOMapper;
    @Override
    public MemberPO getMemberPOByLoginAcct(String loginacct) {
        // 1.创建 Example 对象
        MemberPOExample example = new MemberPOExample();
        // 2.创建 Criteria 对象
        MemberPOExample.Criteria criteria = example.createCriteria();
        // 3.封装查询条件
        criteria.andLoginAcctEqualTo(loginacct);
        // 4.执行查询
        List<MemberPO> list = memberPOMapper.selectByExample(example);
        // 5.获取结果
        return list.get(0);
    }
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void saveMember(MemberPO memberPO) {
        memberPOMapper.insertSelective(memberPO);
    }


    @Override
    public PageInfo<MemberPO> getParseInfo(String k, Integer pageNum, Integer pageSize) {
        //
        // 利用PageHelper的静态方法开启分页
        PageHelper.startPage(pageNum, pageSize);

        // 调用Mapper接口的对应方法
        List<MemberPO> memberPO = memberPOMapper.selectAdminBy(k);

        // 为了方便页面的使用，把Admin的List封装成PageInfo（放别得到页码等数据）
        PageInfo<MemberPO> pageInfo = new PageInfo<>(memberPO);

        // 返回得到的pageInfo对象
        return pageInfo;

    }
    @Override
    public Integer deteid(Integer i, MemberPO admin) {
        if (admin.getId() == i)
            throw new ArithmeticException(CrowdConstant.REMOVE_MY_SELF);
        return memberPOMapper.deleteByPrimaryKey(i);
    }
    @Override
    public void saveAdmin(MemberPO admin) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String formPwd = admin.getUserPswd();
        String encode = bCryptPasswordEncoder.encode(formPwd);
        admin.setUserPswd(encode);
//        Date date = new Date();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String creTime = format.format(date);
//        admin.setCreateTime(creTime);
        try {
            memberPOMapper.insert(admin);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                throw new LoginAcctAlreadyInUseException(CrowdConstant.MESSAGE_SYSTEM_ERROR_LOGIN_NOT_UNIQUE);
            }
        }


    }
    @Override
    public MemberPO getid(Integer i) {

        return memberPOMapper.selectByPrimaryKey(i);
    }
    @Override
    public Integer updateAdmin(MemberPO admin) {
        Integer integer = memberPOMapper.updateByPrimaryKey(admin);
        return integer;
    }

    @Override
    public void saveAdminRoleRelationship(Integer adminId, List<Integer> roleIdList) {
        // 先清除旧的对应inner_admin_role表中对应admin_id的数据
        memberPOMapper.clearOldRelationship(adminId);

        // 如果roleIdList非空，则将该list保存到数据库表中，且admin_id=adminId
        if (roleIdList != null && roleIdList.size() > 0) {
            memberPOMapper.saveAdminRoleRelationship(adminId, roleIdList);
        }
        // roleIdList为空，则清空后不做操作
    }




}
